package com.t.bricks.bussiness.db.dao.votestatistics.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import com.t.bricks.bussiness.db.dao.votestatistics.VoteStatisticsDao;
import com.t.bricks.bussiness.db.entity.votestatistics.VoteStatistics;
import com.t.bricks.bussiness.db.mapper.votestatistics.VoteStatisticsMapper;
import com.t.bricks.bussiness.model.votestatistics.ConditionVoteStatistics;
import com.t.bricks.model.Log;
import com.t.bricks.model.MsgEmity;
import com.t.bricks.model.OrderInfoList;
import com.t.bricks.model.Page;
import com.t.bricks.utils.function.data.IntegerUtil;

@Repository
public class VoteStatisticsDaoImpl implements VoteStatisticsDao {
	
	@Autowired
	private VoteStatisticsMapper voteStatisticsMapper;

	@Value("${app.findPage.dLikeDate:配置文件中的当前库全文检索时间范围dLikeDate未设置}")
	private String sLikeDate;
	
	/**
	 * 查询分页信息
	 * @param page 分页信息对象
	 * @param orders 排序数据集合
	 * @param sLikeStr 全文检索
	 * @param condition 查询条件对象
	 * @return MsgEmity
	 */
	@Override
	public MsgEmity findByPage(Page page, OrderInfoList orders, String sLikeStr,
			ConditionVoteStatistics condition) {
		if ("配置文件中的当前库全文检索时间范围dLikeDate未设置".equals(sLikeDate)) {
			sLikeDate = "3";
		}
		
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.add(Calendar.DATE, - IntegerUtil.strToInt(sLikeDate, 3));//日期加m天
		Date dLikeDateSt = calendar.getTime();
		Date dLikeDateEd = new Date();
		
		int iCount = 0;
		try {
			iCount = voteStatisticsMapper.findByPageCount(sLikeStr, dLikeDateSt, dLikeDateEd, condition);
		} catch (Exception e) {
			Log.error("查询数量发生异常", e.getMessage());
			return new MsgEmity(false, "查询数量失败！", 7001);
		}
		
		if (iCount < 1) {
			return new MsgEmity(true, "没有相关数据！", page);
		}

		page.setCountRow(iCount);

		List<VoteStatistics> list = null;
		try {
			list = voteStatisticsMapper.findByPage(page,
					(null == orders) ? null : orders.getOrderInfoList(),
					sLikeStr, dLikeDateSt, dLikeDateEd, condition);
			if (null == list) {
				Log.error("查询数据发生异常,执行的返回值为:null,预期值不应为null");
				return new MsgEmity(false, "查询操作失败！", 7002);
			}
		} catch (Exception e) {
			Log.error("查询分页数据发生异常:", e.getMessage());
			return new MsgEmity(false, "查询请求失败，请查看异常日志！", 7003);
		}

		page.setRecordList(list);
		
		if (null != sLikeStr) {
			return MsgEmity.success(page, "查询成功！", "由于使用了全文检索,因此数据查询限制为近", sLikeDate, "天创建的数据");
		}
		
		return new MsgEmity(true, "查询成功！", page);
	}
	
	/**
	 * 根据记录编号查询符合分页数据的某条记录
	 * @param sId 记录编号
	 * @return MsgEmity
	 */
	@Override
	public MsgEmity findByPageRow(String sId) {
		VoteStatistics voteStatistics = null;
		try {
			voteStatistics = voteStatisticsMapper.findByPageRow(sId);
			if (null == voteStatistics) {
				return new MsgEmity(false, "数据不存在！", 7001);
			}
		} catch (Exception e) {
			Log.error("查询发生异常:", e.getMessage());
			return new MsgEmity(false, "查询失败！", 7002);
		}
		
		return new MsgEmity(true, "查询成功！", voteStatistics);
	}
	
	/**
	 * 查询搜索用的数据
	 * 警告:本方法只允许内部使用,不能开放到外部接口
	 * @param whereStr 查询条件字符串
	 * @param orders 排序数据集合
	 * @return MsgEmity
	 */
	@Override
	public MsgEmity findBySearch(String whereStr, OrderInfoList orders) {
		if (null == whereStr || "".equals(whereStr.trim())) {
			return new MsgEmity(false, "查询条件不允许为空！", 7001);
		}

		List<VoteStatistics> list = null;
		try {
			list = voteStatisticsMapper.findBySearch(whereStr.trim(),
					(null == orders) ? null : orders.getOrderInfoList());
			if (null == list) {
				Log.error("查询数据发生异常,执行的返回值为:null,预期值不应为null");
				return new MsgEmity(false, "查询操作失败！", 7002);
			}
		} catch (Exception e) {
			Log.error("查询分页数据发生异常:", e.getMessage());
			return new MsgEmity(false, "查询请求失败，请查看异常日志！", 7003);
		}
		
		return new MsgEmity(true, "查询成功！", list);
	}

	/**
	 * 投票
	 * @param sVoteRecordId
	 * @param sTotalPoll
	 * @param sOption
	 * @return
	 */
	@Override
	public MsgEmity vote(String sVoteRecordId, String sUserIds, String sTotalPoll, String sTotalOptions) {
		try {
			int i = voteStatisticsMapper.vote(sVoteRecordId, sUserIds, sTotalPoll, sTotalOptions);
			if (i < 1) {
				Log.error("投票操作未能得到预期影响值(预期1):", i);
				return new MsgEmity(false, "投票操作失败！", 7001);
			}
		} catch (Exception e) {
			Log.error("投票操作发生异常:", e.getMessage());
			return new MsgEmity(false, "投票操作失败！", 7002);
		}
		
		return new MsgEmity(true, "查询成功！", sVoteRecordId);
	}
	
	/**
	 * 查找当前投票数据信息
	 * @param sId
	 * @return
	 */
	@Override
	public MsgEmity findVoteData(String sId) {
		VoteStatistics voteStatistics = null;
		try {
			voteStatistics = voteStatisticsMapper.findVoteData(sId);
			if (null == voteStatistics) {
				return new MsgEmity(false, "数据不存在！", 7001);
			}
		} catch (Exception e) {
			Log.error("查询发生异常:", e.getMessage());
			return new MsgEmity(false, "查询失败！", 7002);
		}
		
		return new MsgEmity(true, "查询成功！", voteStatistics);
	}
	
}
